package com.zyj.benchmark.sample;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
public class Sample_23_AuxCounters {


//    @State(Scope.Thread)
//    @AuxCounters(  //专门放在@State对象上;Type有两种:
//            AuxCounters.Type.OPERATIONS // 1.OPERATIONS: 报表吞吐量;
//    )
//    public static class OpCounters {
//        // These fields would be counted as metrics
//        public int case1;  //输出表格
//        public int case2;  //输出表格
//
//        // This accessor will also produce a metric
//        public int total() {
//            return case1 + case2;
//        }
//    }
//
//
//    @Benchmark
//    public void splitBranch(OpCounters counters) {
//        if (Math.random() < 0.1) { //取值范围 [0,1)
//            counters.case1++; //P=0.1
//        } else {
//            counters.case2++; //P=0.9
//        }
//    }


    @State(Scope.Thread)
    @AuxCounters(
            AuxCounters.Type.EVENTS //2.EVENTS:报表展示计数;
    )
    public static class EventCounters {
        // This field would be counted as metric
        public int wows;
    }

    @Benchmark
    public void runSETI(EventCounters counters) {
        float random = (float) Math.random();
        float wowSignal = (float) Math.PI / 4; //概率很低
        if (random == wowSignal) {
            // WOW, that's unusual.
            counters.wows++;
        }
    }


    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(Sample_23_AuxCounters.class.getSimpleName())
                .build();

        new Runner(opt).run();
    }
}
